/*
    YmsgIRC - Program enabling YMSG chat clients to use IRC
    Copyright (C) 2011 Crash Daemonicus ( crashenator -at- gmail.com )

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "YMSG_packet.h"
/*
YmsgPacket::YmsgPacket(                               int typ,int sta,int sid,int vid,int ver)
{
	content="";
	contentObject.type=0;
	contentObject.fields.clear();
	head.set(typ,0,sta,sid,vid,ver); 
}

YmsgPacket::YmsgPacket(const YmsgHead& head2,const string& fields)
{
	head=head2;
	content=fields;
	contentObject=YmsgItem(0,content);
}
YmsgPacket::YmsgPacket(const string& fields,          int typ,int sta,int sid,int vid,int ver)
{
	content=fields;
	contentObject=YmsgItem(0,content);
	head.set(typ,content.length(),sta,sid,vid,ver);
}
*/
YmsgPacket::YmsgPacket(const vector<YmsgField>& fields,int typ,int sta,int sid,int vid,int ver)
{
	contentObject=YmsgItem(0,fields);
	content=contentObject.toString();
	head.set(typ,content.length(),sta,sid,vid,ver);
}
YmsgPacket::YmsgPacket(const YmsgItem& fields,        int typ,int sta,int sid,int vid,int ver)
{
	contentObject=fields;
	content=contentObject.toString();
	head.set(typ,content.length(),sta,sid,vid,ver);
}

int YmsgPacket::size(){ return 20+head.Ref.contentLength; }

bool YmsgPacket::toBuf(char* buffer,int maxlen)
{
	int len=20+head.Ref.contentLength;
	if(maxlen<len) return false;
	memcpy(buffer, &head.Dat, 20);
	if(head.Ref.contentLength) memcpy(buffer+20, content.c_str(), head.Ref.contentLength);
	return true;
}